package com.livrexpress.livrexpressandroid.activity;

import java.util.ArrayList;
import java.util.List;

import com.mapquest.android.maps.GeoPoint;
import com.mapquest.android.maps.MapActivity;
import com.mapquest.android.maps.MapView;
import com.mapquest.android.maps.MyLocationOverlay;
import com.mapquest.android.maps.RouteManager;
import com.mapquest.android.maps.RouteResponse;
import com.mapquest.android.maps.ServiceResponse.Info;
import com.livrexpress.livrexpressandroid.activity.R;
import com.livrexpress.livrexpressandroid.localdomain.Delivery;
import com.livrexpress.livrexpressandroid.localdomain.Person;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.webkit.WebView;
import android.widget.Toast;

public class DeliveryMapActivity extends MapActivity {
	protected MapView myMap;
	protected String destination = null;
	protected WebView itinerary = null;
	private LocationManager locManager;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);
		
        Bundle extra = getIntent().getExtras();
        
        if (extra.getString("deliveryId") != null ){
        	String deliveryId = extra.getString("deliveryId");
        	Delivery d = Delivery.getDelivery(deliveryId);
            Person dest = Person.getPerson(d.getIdReceiver());
            destination = dest.getCity() + ", fr";
            setupMapView();
        } 
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.map, menu);
		return true;
	}
	
	private void setupMapView() {
		this.myMap = (MapView) findViewById(R.id.map);
		this.itinerary =(WebView)findViewById(R.id.itinerary);
		//this.myMap.getController().setCenter(new GeoPoint(46.713618, 3.139706));
		//this.myMap.getController().setZoom(6);
		// enable the zoom controls
		myMap.setBuiltInZoomControls(true);
	}
	
	
	@Override
	protected void onResume() {
		if(destination != null){
			setUpRoute();
		}
		super.onResume();
	}
	
	protected void setUpRoute(){
		locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
		Criteria criteria = new Criteria();                    
		criteria.setAltitudeRequired(false);
		criteria.setBearingRequired(false);
		criteria.setCostAllowed(true);
		criteria.setSpeedRequired(false);
		criteria.setPowerRequirement(Criteria.POWER_LOW);                      
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		String providerFine = locManager.getBestProvider(criteria, true);
		Location location = locManager.getLastKnownLocation(providerFine);
		String from = location.getLatitude() + "," + location.getLongitude();
		
		RouteManager routeManager = new RouteManager(this);
		routeManager.setMapView(myMap);
		routeManager.setItineraryView(itinerary);
		routeManager.setDebug(true);
		//routeManager.setIgnoreAmbiguities(true);
		
		routeManager.setRouteCallback(new RouteManager.RouteCallback() {
			@Override
			public void onError(RouteResponse routeResponse) {
				Info info=routeResponse.info;
				int statusCode=info.statusCode;
				
				StringBuilder message =new StringBuilder();
				message.append("Unable to create route.\n")
					.append("Error: ").append(statusCode).append("\n")
					.append("Message: ").append(info.messages);
				Toast.makeText(DeliveryMapActivity.this, message.toString(), Toast.LENGTH_LONG).show();
			}

			@Override
			public void onSuccess(RouteResponse routeResponse) {
			}
		});
		
		List<String> points = new ArrayList<String>();
		points.add(from);
		points.add(destination);
		routeManager.createRoute(points);
		
//		listener = new MyLocationListener();
//		listener.setListener(this, myMap, itinerary, destination);
//		locManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, listener);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
//	protected void setupMyLocation() {
//	this.myLocationOverlay = new MyLocationOverlay(this, myMap);
//	
//	myLocationOverlay.enableMyLocation();
//	myLocationOverlay.runOnFirstFix(new Runnable() {
//		@Override
//		public void run() {			
//			myLocationOverlay.setFollowing(true);
//			String from = myLocationOverlay.getMyLocation().toString();
//			from = from.split(":")[1].trim();
//			RouteManager routeManager = new RouteManager(DeliveryMapActivity.this);
//			routeManager.setMapView(myMap);
//			routeManager.setItineraryView(itinerary);
//			routeManager.setDebug(true);
//			//routeManager.setIgnoreAmbiguities(true);
//			routeManager.setRouteCallback(new RouteManager.RouteCallback() {
//				@Override
//				public void onError(RouteResponse routeResponse) {
//					Info info=routeResponse.info;
//					int statusCode=info.statusCode;
//					
//					StringBuilder message =new StringBuilder();
//					message.append("Unable to create route.\n")
//						.append("Error: ").append(statusCode).append("\n")
//						.append("Message: ").append(info.messages);
//					Toast.makeText(getApplicationContext(), message.toString(), Toast.LENGTH_LONG).show();
//				}
//
//				@Override
//				public void onSuccess(RouteResponse routeResponse) {
//				}
//			});
//			
//			List<String> points = new ArrayList<String>();
//			points.add(from);
//			points.add(destination);
//			routeManager.createRoute(points);
//			
//			
//		}
//	});
//	//myLocationOverlay.setFollowing(true);
//}
	
}
